package com.game.ctrler
{
	import base.BaseHero;
	import base.BaseMonster;
	import base.BaseObject;
	
	import com.dusk.game.Logger;
	import com.game.data.vo.GameGadgetVO;
	
	import com.game.engine.MainGame;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.view.component.GameGadgetType;
	
	import config.Config;
	import flash.display.Sprite;
	
	public class GameViewCtrler
	{
		public var root:Sprite;
		
		private var mainGame:MainGame;
		
		private function get gc():Config
		{
			return Config.getIns();
		}
		
		public function GameViewCtrler(game:MainGame)
		{
			mainGame = game;
			root = mainGame.root;
			init();
		}
		
		public function init():void
		{
			EventManager.getIns().registerListen(GameEventType.AURA_DESTROY,auraDestroy);
			EventManager.getIns().registerListen(GameEventType.REFRESH_SKILL,refreshSkill);
			EventManager.getIns().registerListen(GameEventType.REFRESH_PET,refreshPet);
			EventManager.getIns().registerListen(GameEventType.MONSTER_BE_HIT,monsterBeHit);
			EventManager.getIns().registerListen(GameEventType.MONSTER_BE_HIT,addCombo);
			EventManager.getIns().registerListen(GameEventType.FREEZE_FRAME,freezeFrame);
			EventManager.getIns().registerListen(GameEventType.ADD_GAME_GADGET,addGameGadget);
		}
		
		public function destroy():void
		{
			EventManager.getIns().removeListen(GameEventType.AURA_DESTROY,auraDestroy);
			EventManager.getIns().removeListen(GameEventType.REFRESH_SKILL,refreshSkill);
			EventManager.getIns().removeListen(GameEventType.REFRESH_PET,refreshPet);
			EventManager.getIns().removeListen(GameEventType.MONSTER_BE_HIT,monsterBeHit);
			EventManager.getIns().removeListen(GameEventType.MONSTER_BE_HIT,addCombo);
			EventManager.getIns().removeListen(GameEventType.FREEZE_FRAME,freezeFrame);
			EventManager.getIns().removeListen(GameEventType.ADD_GAME_GADGET,addGameGadget);
			root = null;
			mainGame = null;
		}
		
		public function refreshPet():void
		{
			gc.gameInfo.roleInfo.addPetHead();
		}
		
		public function addGameGadget(data:Object):void
		{
			var vo:GameGadgetVO = data as GameGadgetVO;
			mainGame.gadgetQueue.addGadgetToQueue(vo);
		}
		
		public function refreshSkill():void
		{
			gc.gameInfo.roleInfo.setSkillIcon();
		}
		
		public function auraDestroy(data:Array):void
		{
			var sourceRole:BaseObject = data[0] as BaseObject;
			var hpCure:int = data[1] as int;
			var mpCure:int = data[2] as int;
			var soulValue:int = data[3] as int;
			var energy:int = data[4] as int;
			if(!sourceRole || sourceRole.isDead() || sourceRole.isReadyToDestroy)
			{
				return;
			}
			if(sourceRole is BaseHero)
			{
				with(sourceRole as BaseHero)
				{
					if (cureHp > 0) cureHp(hpCure);
					if (cureMp > 0) cureMp(mpCure);
					if (soulValue > 0) getPlayer().addLhValue(soulValue);
					if (!isGXP && energy > 0)
					{
						getAttribute().energy += energy;
					}
				}
			}
			else if(sourceRole is BaseMonster)
			{
				if(hpCure)
				{
					(sourceRole as BaseMonster).cureHp(hpCure);
				}
			}
		}
		
		public function monsterBeHit(data:Array):void
		{
			var hurt:int = data[0] as int;
			var sourceRole:BaseHero = data[1] as BaseHero;
		}
		
		public function addCombo(data:Array):void
		{
			gc.gameInfo.batterPanel();
		}
		
		public function freezeFrame(data:int):void
		{
			mainGame.freezeFrame(data);
		}
		
		public function getMinIdxInHeroAndPet():int
		{
			var idx:int = 0;
			var minIdx:int = 999;
			for each (var obj:BaseObject in gc.team.getHeroAndPetArray())
			{
				if (obj.parent == gc.gameScene)
				{
					idx = gc.gameScene.getChildIndex(obj);
					if (idx != -1)
					{
						minIdx = minIdx > idx ? idx : minIdx;
					}
				}
			}
			if (minIdx == 999)
			{
				minIdx = 0;
			}
			return minIdx;
		}
	}
}
